package com.xbongbong.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.pro.user.pojo.ReturnUserAndDepartmentPojo;
import com.xbongbong.saas.constant.BasicConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/15 10:42
 */
public class OwnerIdAndCoUserIdUtil {

    public static Map<String, List<ReturnUserAndDepartmentPojo>> setOwnIdAndCoUserId(JSONObject data) {
        // 解析新版协同人和负责人
        Map<String, List<ReturnUserAndDepartmentPojo>> resultMap = new HashMap<>(BasicConstant.TWO);
        JSONArray ownIdList = data.getJSONArray(FieldTypeEnum.OWNERID.getAlias());
        JSONArray coUserIdList = data.getJSONArray(FieldTypeEnum.COUSERID.getAlias());
        List<ReturnUserAndDepartmentPojo> ownList = new ArrayList<>();
        if (!Objects.isNull(ownIdList)) {
            for (Object userId : ownIdList) {
                ReturnUserAndDepartmentPojo returnUserAndDepartmentPojo = new ReturnUserAndDepartmentPojo(userId);
                ownList.add(returnUserAndDepartmentPojo);
            }
        }
        resultMap.put(FieldTypeEnum.OWNERID.getAlias(), ownList);
        List<ReturnUserAndDepartmentPojo> coUserList = new ArrayList<>();
        if (!Objects.isNull(coUserIdList)) {
            for (Object userId : coUserIdList) {
                ReturnUserAndDepartmentPojo returnUserAndDepartmentPojo = new ReturnUserAndDepartmentPojo(userId);
                coUserList.add(returnUserAndDepartmentPojo);
            }
        }
        resultMap.put(FieldTypeEnum.COUSERID.getAlias(), coUserList);
        return resultMap;
    }


    public static void setOwnIdAndCoUserId(JSONObject data, FormDataAddDTO formDataAddDTO, FormDataUpdateDTO formDataUpdateDTO) {
        // 解析新版协同人和负责人
        JSONArray ownIdList = data.getJSONArray(FieldTypeEnum.OWNERID.getAlias());
        JSONArray coUserIdList = data.getJSONArray(FieldTypeEnum.COUSERID.getAlias());
        if ( !Objects.isNull(ownIdList) ) {
            List<ReturnUserAndDepartmentPojo> ownList = new ArrayList<>();
            for ( Object userId : ownIdList ) {
                ReturnUserAndDepartmentPojo returnUserAndDepartmentPojo = new ReturnUserAndDepartmentPojo();
                returnUserAndDepartmentPojo.setId(Objects.toString(userId, ""));
                ownList.add(returnUserAndDepartmentPojo);
            }
            if ( Objects.nonNull(formDataAddDTO) ) {
                formDataAddDTO.setOwnerId(ownList);
            } else if ( Objects.nonNull(formDataUpdateDTO) ) {
                formDataUpdateDTO.setOwnerId(ownList);
            }
        }
        if ( !Objects.isNull(coUserIdList) ) {
            List<ReturnUserAndDepartmentPojo> coUserList = new ArrayList<>();
            for ( Object userId : coUserIdList ) {
                ReturnUserAndDepartmentPojo returnUserAndDepartmentPojo = new ReturnUserAndDepartmentPojo();
                returnUserAndDepartmentPojo.setId(Objects.toString(userId, ""));
                coUserList.add(returnUserAndDepartmentPojo);
            }
            if ( Objects.nonNull(formDataAddDTO) ) {
                formDataAddDTO.setCoUserId(coUserList);
            } else if ( Objects.nonNull(formDataUpdateDTO) ) {
                formDataUpdateDTO.setCoUserId(coUserList);
            }
        }
    }
}
